package com.hjm.project.ShopOrder.ShopOrder.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.project.ShopOrder.ShopOrder.api.ExpressBirdApi;
import com.hjm.project.ShopOrderBreak.ShopOrderBreak.domain.ShopOrderBreak;
import com.hjm.project.ShopOrderBreak.ShopOrderBreak.mapper.ShopOrderBreakMapper;
import com.hjm.project.agentInfo.agentInfo.mapper.AgentInfoMapper;
import com.hjm.project.terminal.record.mapper.TerminalRecordMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.ShopOrder.ShopOrder.mapper.ShopOrderMapper;
import com.hjm.project.ShopOrder.ShopOrder.domain.ShopOrder;
import com.hjm.project.ShopOrder.ShopOrder.service.IShopOrderService;
import com.hjm.common.utils.text.Convert;

/**
 * 订单Service业务层处理
 * 
 * @author ShopOrder
 * @date 2020-11-30
 */
@Service
public class ShopOrderServiceImpl implements IShopOrderService 
{
    @Autowired
    private ShopOrderMapper shopOrderMapper;

    @Autowired
    private ShopOrderBreakMapper shopOrderBreakMapper;

    @Autowired
    private TerminalRecordMapper terminalRecord;

    @Autowired
    private AgentInfoMapper agentInfoMapper;

    /**
     * 查询订单
     * 
     * @param serialLogo 订单ID
     * @return 订单
     */
    @Override
    public ShopOrder selectShopOrderById(Long serialLogo)
    {
        return shopOrderMapper.selectShopOrderById(serialLogo);
    }

    /**
     * 查询订单列表
     * 
     * @param shopOrder 订单
     * @return 订单
     */
    @Override
    public List<ShopOrder> selectShopOrderList(ShopOrder shopOrder)
    {
        return shopOrderMapper.selectShopOrderList(shopOrder);
    }

    /**
     * 查询订单
     *
     * @param orderNo 订单ID
     * @return 订单
     */
    @Override
    public ShopOrder selectShopOrderByIds(String orderNo)
    {
        HashMap map = new HashMap();
        map.put("orderNo",orderNo);
        return shopOrderMapper.selectShopOrderByIds(map);
    }

    /**
     * 新增订单
     * 
     * @param shopOrder 订单
     * @return 结果
     */
    @Override
    public int insertShopOrder(ShopOrder shopOrder)
    {
        return shopOrderMapper.insertShopOrder(shopOrder);
    }

    /**
     * 修改订单
     * 
     * @param shopOrder 订单
     * @return 结果
     */
    @Override
    public int updateShopOrder(ShopOrder shopOrder)
    {
        return shopOrderMapper.updateShopOrder(shopOrder);
    }

    /**
     * 删除订单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteShopOrderByIds(String ids)
    {
        return shopOrderMapper.deleteShopOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单信息
     * 
     * @param serialLogo 订单ID
     * @return 结果
     */
    @Override
    public int deleteShopOrderById(Long serialLogo)
    {
        return shopOrderMapper.deleteShopOrderById(serialLogo);
    }


    /**
     * 发货
     * @param orderBreak 物流基本信息
     * @return 结果
     */
    @Override
    public int delivery(ShopOrderBreak orderBreak) throws Exception {
        //通过订单号获取订单下的所有子订单
        ShopOrder so = new ShopOrder();
        so.setOrderNo(orderBreak.getOrderNo());
        List<ShopOrder> shopOrders = shopOrderMapper.selectShopOrderLists(so);
        if (CollectionUtils.isEmpty(shopOrders)) {
            throw  new BusinessException("错误的订单信息");
        }
        ShopOrder shopOrder = shopOrders.get(0);
        if (shopOrder.getOrderStatus() != 3) {
            throw  new BusinessException("当前订单不是待发货状态");
        }
        orderBreak.setUpdateDate(DateUtils.getDateTime());
        //添加发货记录
        int i = shopOrderBreakMapper.insertWl(orderBreak);
        //获取添加成功的主键ID作为 订单明细表中的商品物流关联依据
        Integer id = orderBreak.getId();
        //更新当前订单的发货ID
        ShopOrder orderDetail = new ShopOrder();
        orderDetail.setOrderNo(orderBreak.getOrderNo());
        //更新物流信息
        orderDetail.setBreakId(Long.parseLong(id+""));
        //更新订单状态
        orderDetail.setOrderStatus(Long.parseLong("1"));
        int i1 = shopOrderMapper.updateShopOrder(orderDetail);
        if (i1 <= 0 ) {
            throw  new BusinessException("发货失败");
        }
        return shopOrder.getGoodsNum().intValue();
    }


    @Override
    public List<Map<String, Object>> queryOrder(ShopOrderBreak orderBreak){
        List<Map<String, Object>> send = null;
        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setOrderNo(orderBreak.getOrderNo());
        //获取物流信息
        List<ShopOrderBreak> shopOrderBreaks = shopOrderBreakMapper.selectShopOrderBreakList(orderBreak);
        if (CollectionUtils.isNotEmpty(shopOrderBreaks)) {
            ShopOrderBreak shopOrderDetail1 = shopOrderBreaks.get(0);
            //快递Id
            String expressId = shopOrderDetail1.getExpressId();
            //快递单号
            String expressOrderNo = shopOrderDetail1.getExpressOrderNo();
            //快递鸟
            send = new ExpressBirdApi().getData(expressId,expressOrderNo);
            //快递100
            //send = Express.send(expressId, expressOrderNo);
            if (CollectionUtils.isEmpty(send)) {
                throw new BusinessException("暂无物流信息");
            }
        } else {
            throw new BusinessException("暂无物流信息");
        }
        return send;
    }


}
